<template>
  <div class="cart">
    <div class="cart-header border-bottom">
      <div class="back-icon" @click="goBack">
        <i class="el-icon-arrow-left">返回</i>
      </div>
      <div class="cart-title">
        <p>购物车</p>
      </div>
      <div class="cart-ctrl">
        <el-checkbox :indeterminate="isIndeterminate" v-model="checkAll"  @change="checkedAll">全选</el-checkbox>
      </div>
    </div>
    <div class="cart-content">
      <div class="content-wrapper">
        <div class="item-wrapper border-bottom" v-for="item in cartList" :key="item.id">
          <div class="item item-check">
            <el-checkbox  v-model="item.checked" @change="handleCheckedCartChange"></el-checkbox>
          </div>
          <div class="item item-img">
            <img :src="item.imgUrl" alt="">
          </div>
          <div class="item item-details">
            <div class="goods-price">
              <div class="item-title">
                <span>{{ item.title }}</span>
              </div>
              <div class="item-price">
                <span>¥ <span class="current_price">{{ item.current_price }}</span></span>
              </div>
            </div>
            <div class="good-amount">
              <div class="goods-type">
                <span>{{ item.desc }}</span>
              </div>
              <div class="goods-inputnumber">
                <el-input-number v-model="item.amount" @change="handleChange" :min="1"  :max="20" size="mini" label="描述文字"></el-input-number>
              </div>
            </div>
          </div>
        </div>
      </div>
      <div class="test">
        <p v-for="item of t" :key="item.id">
          {{ item }}
        </p>
      </div>
    </div>
    <div class="cart-footer border-top">
      <div class="total">
        <span>合计￥</span>
        <span>{{ this.totalMoney }}</span>
      </div>
      <div class="sub-btn" @click="calcAll">
        <span>提交订单</span>
      </div>
    </div>
  </div>
</template>

<script>
  const cartOptions = [
    {
      id: '1',
      imgUrl: 'http://www.gstate.cn/static/img/tiancai.808dfd5.jpg',
      title: '人性的弱点完整全译本 (美)戴尔·卡耐基(Dale Carnegie)著',
      current_price: '32',
      amount: '2',
      checked: false
    },
    {
      id: '2',
      imgUrl: 'http://www.gstate.cn/static/img/tiancai.808dfd5.jpg',
      title: '【现货正版包邮】 天才在左疯子在右 完整版 高铭著',
      current_price: '35',
      amount: '2',
      checked: false
    },
    {
      id: '3',
      imgUrl: 'http://www.gstate.cn/static/img/tiancai.808dfd5.jpg',
      title: '【现货正版包邮】 天才在左疯子在右 完整版 高铭著',
      current_price: '31',
      amount: '10',
      checked: false,
    }
  ];
  export default {
    name: "Cart",
    data () {
      return {
        checkAll: false,
        isIndeterminate: false,  // 控制样式状态
        amount: 1,
        cartList: cartOptions,
        cartLength: 0,  // 购物车的选中个数
        isCartChecked:[],
        totalMoney: 0, //结算下方初始金额
        t: []
      }
    },
    computed: {
      stateCart () {
        return this.$store.state.cart
      }
    },
    methods: {
      /* 计算选中内容的总价钱 */
      calc () {
        /* 筛选出勾选的选项 */
        let self = this;
        let r;
        this.cartList.forEach((item, index)=> {
          self.isCartChecked = [];
          r = this.cartList.filter(item => item.checked ===true)
        });
        self.isCartChecked.push(r);
        console.log(this.isCartChecked);
        /* 计算勾选内容的总价格 */
        // let all_Price = 0
        this.totalMoney = 0;
        this.isCartChecked[0].forEach((item, index)=> {
          if (item.checked) {
            self.totalMoney += item.amount* item.current_price
          }
        });
        console.log(self.totalMoney)
      },
      goBack () {
        this.$router.push('./personal_index');
      },
      /* 计数器
       * 1、当数值发生变化时，对应的金额会发生变化。
       */
      handleChange(value) {
        console.log(value);
        this.calc()
      },
      /* 监听多选按钮
       * 1、监听按钮的选中个数，全部选中时触发全选按钮的样式变化。
       * 2、选中按钮后，合计的金额对应发生变化
       */
      handleCheckedCartChange(value) {
        let self = this;
        let objKeys = Object.keys(this.cartList).length;  //计算对象长度
        /* 判断是否全部选中，触发全选按钮样式变化 */
        if (value === true) {
          self.cartLength = self.cartLength + 1
        } else {
          self.cartLength = self.cartLength - 1;
          if (self.cartLength < 0) {
            self.cartLength = objKeys - 1
          }
        }
        let checkedCount = self.cartLength;
        this.checkAll = checkedCount === objKeys;
        this.isIndeterminate = checkedCount > 0 && checkedCount < objKeys;
        /* 计算选中按钮的金额 */
        this.calc()
      },
      /* 全选按钮 */
      checkedAll (val) {
        let self = this;
        if(val === true) {
          self.cartList.forEach((item, index)=>{
            item.checked = true
          });
          self.cartLength = self.cartList.length
          self.isIndeterminate = false;
        } else {
          self.cartList.forEach((item, index)=>{
            item.checked = false
          });
          self.cartLength = 0;
          self.checkAll = false ;
        }
        /* 计算勾选内容的总价格 */
        this.calc()
      },
      /* 结算按钮 */
      calcAll () {
        alert('去结算吧！');
      }
    }
  }
</script>

<style lang="stylus" scoped>
  /* header */
  .cart-header {
    width: 100%;
    height: 50px;
    background: #fff;
  }
  .cart-title,.back-icon, .cart-ctrl {
    float: left;
    color: #666;
    text-align: center;
  }
  .back-icon {
    width: 20%;
    line-height: 50px;
    font-size: 16px;
    z-index: 2;
    color: #1989fa;
  }
  .cart-title {
    width: 60%;
    line-height: 50px;
    font-size: 18px;
  }
  .cart-title p {
    margin: 0;
  }
  .cart-ctrl {
    width: 20%;
    line-height: 50px;
    text-decoration: underline;
    font-size: 14px;
    color: #333;
  }
  /* content */;
  .item-check >>> .el-checkbox__input.is-checked .el-checkbox__inner {
    border-radius: 50%;
  }
  .item-wrapper, .goods-price, .good-amount   {
    display: flex;
    padding: .2rem 0;
  }
  .item-check {
    text-align: center;
    display: flex;
    align-items: center;
    padding-left: 10px;
  }
  .item-img, .item-title, .item-price {
    padding: .1rem;
    font-size: 14px;
    font-family: sans-serif;
    font-weight: 400
  }
  .item-img {
    width: 20%;
  }
  .item-img img {
    width: 100%
  }
  .item-details {
    width: 80%;
  }
  .goods-price {
    position: relative;
  }
  .item-title {
    width: 80%;
  }
  .item-price {
    width: 20%;
    text-align: center;
    position: absolute;
    bottom: 0
    right: 0
  }
  .item-price span {
    color: #666;
    font-size: 12px;
  }
  .item-price span span.current_price{
    font-weight: 500
    font-size: 12px;
    color: #333;
  }
  .goods-type{
    font-size: 12px;
    color: #888;
    line-height: 24px;
    margin-right: 20px;
  }
  /* footer */
  .cart-footer {
    position: absolute;
    bottom: 0;
    left: 0;
    right: 0;
    display: flex;
  }
  .total {
    flex: 3;
    text-align: right;
    padding: 15px 10px;
    background: #3a3a3a;
    color: #fff;
  }
  .total span:first-child {
    font-size: 12px;
    font-family: sans-serif;
    font-weight: 400;
    letter-spacing: 1px;
  }
  .total span:last-child {
    font-size: 16px;
    font-family: sans-serif;
    font-weight: 500;
  }
  .sub-btn {
    flex: 1;
    padding: 15px 0;
    text-align: center;
    background: gold;
    color: #3a3a3a;
    font-size: 14px;
    font-family: sans-serif;
    font-weight: 600;
    line-height: 18px;
  }
</style>
